<?php
	require_once 'php/sqlfunc.php';
	
	if(isset($_POST['submit'])){
		$imgFile=$_FILES['upload'];
		uploadimg($imgFile,$_POST['imgdes']);				
	}
	if(isset($_POST['favor'])){
		addFavor($_POST['img_id'],'userid');
	}
	
?>
<html>
<head>
<title>Ima App Demo Test1</title>
<script type="text/javascript">
	var xmlHttp;
	function getXmlHttpObj(){
		var xmlHttp=null;
		try{
			xmlHttp=new XMLHttpRequest();
			}catch(e){
				try{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
					}catch(e){
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
						}
				}
		return xmlHttp;
	}
			function favor(){
				alert('Ajax Used');
				//here goes the ajax code
				var xmlHttp=getXmlHttpObj();
				xmlHttp.onreadystatechange=stateChanged;
				var url="./php/addfavor.php?img_id="+"1";
				xmlHttp.open("GET",url,true);
				xmlHttp.send();		
			}

			function stateChanged(){
				var img_root=document.getElementById("img_root");
			}
		
</script>
</head>
<body>
<form action="main.php" method="POST" enctype="multipart/form-data">
<input type="file" name="upload" />
<input type="text" name="imgdes" /><br/>
<input type="submit" name="submit" value="submit"/>
<label>
<input type="reset" value="Reset" />
</label>
</form>
<div id="mes_root">
	<?php 
		$query="select * from imgfile";
		$result=@mysql_query($query);
		//echo "<img src=\"images/1348035321_ass.PNG\" />";
		while($obj=mysql_fetch_object($result)){
			$favorCount=getFavor($obj->id);
			echo "<div><img src=\"$obj->path\" alt=\"$obj->id\"/><div><a href=\"#\">$favorCount</a></div>";
			//echo "<form action=\"main.php\" method=\"POST\"><input type=\"hidden\" name=\"img_id\" value=\"$obj->id\"><input type=\"submit\" name=\"favor\" value=\"Favor\" ></form></div>";
			echo "<input type=\"button\" name=\"favor\" value=\"Favor\" onclick=\"javascript:favor()\">
				  <input type=\"button\" name=\"normal\" value=\"Normal\" onclick=\"javascript:normal()\">
				  <input type=\"button\" name=\"hate\" value=\"Hate\" onclick=\"javascript:hate()\"></div>";
		}
	?>
</div>
</body>
</html>